using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
namespace CommandLib.Base
{
	public class HttpHelper
	{
		public delegate void dgtProgValueChanged(long Value);
		private long m_ProgValue = 0L;
		public event HttpHelper.dgtProgValueChanged OnProgValueChanged;
		public WebProxy Proxy
		{
			get;
			set;
		}
		public CookieContainer UserCookie
		{
			get;
			set;
		}
		public int IAfreshTime
		{
			get;
			set;
		}
		public int IErrorTime
		{
			get;
			private set;
		}
		public long ProgValue
		{
			get
			{
				return this.m_ProgValue;
			}
			private set
			{
				this.m_ProgValue = value;
				if (this.OnProgValueChanged != null)
				{
					this.OnProgValueChanged(value);
				}
			}
		}
		public long ProgMaximum
		{
			get;
			private set;
		}
		public string GetHTML(string URL, string Accept)
		{
			return this.GetHTML(URL, Accept, Encoding.UTF8);
		}
		public string GetHTML(string URL, string Accept, Encoding encoding)
		{
			return this.GetHTML(URL, Accept, encoding, 1024);
		}
		public string GetHTML(string URL, string Accept, Encoding encoding, int bufflen)
		{
			this.IErrorTime = 0;
			return this._GetHTML(URL, Accept, encoding, bufflen);
		}
		private string _GetHTML(string URL, string Accept, Encoding encoding, int bufflen)
		{
			string result;
			try
			{
				HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(URL);
				httpWebRequest.Proxy = this.Proxy;
				httpWebRequest.Accept = Accept;
				if (this.UserCookie == null)
				{
					this.UserCookie = new CookieContainer();
				}
				httpWebRequest.CookieContainer = this.UserCookie;
				HttpWebResponse myResponse = (HttpWebResponse)httpWebRequest.GetResponse();
				result = this._GetHTML(myResponse, encoding, bufflen);
			}
			catch (Exception ex)
			{
				if (!ex.Message.Contains("连接") || this.IAfreshTime - this.IErrorTime <= 0)
				{
					throw;
				}
				this.IErrorTime++;
				result = this._GetHTML(URL, Accept, encoding, bufflen);
			}
			return result;
		}
		private string _GetHTML(HttpWebResponse MyResponse, Encoding encoding, int bufflen)
		{
			string result;
			using (Stream responseStream = MyResponse.GetResponseStream())
			{
				this.ProgMaximum = MyResponse.ContentLength;
				long num = 0L;
				byte[] array = new byte[bufflen];
				int i = responseStream.Read(array, 0, array.Length);
				List<byte> list = new List<byte>(1024);
				while (i > 0)
				{
					num = (long)i + num;
					for (int j = 0; j < i; j++)
					{
						list.Add(array[j]);
					}
					this.ProgValue = num;
					i = responseStream.Read(array, 0, array.Length);
				}
				string @string = encoding.GetString(list.ToArray());
				responseStream.Close();
				result = @string;
			}
			return result;
		}
		public Bitmap Getimg(string URL, string Accept)
		{
			return this._GetBit(URL, Accept);
		}
		private Bitmap _GetBit(string URL, string Accept)
		{
			HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(URL);
			httpWebRequest.Proxy = this.Proxy;
			httpWebRequest.Accept = Accept;
			if (this.UserCookie == null)
			{
				this.UserCookie = new CookieContainer();
			}
			httpWebRequest.CookieContainer = this.UserCookie;
			HttpWebResponse myResponse = (HttpWebResponse)httpWebRequest.GetResponse();
			return this._GetBit(myResponse);
		}
		private Bitmap _GetBit(HttpWebResponse MyResponse)
		{
			Bitmap result;
			using (Stream responseStream = MyResponse.GetResponseStream())
			{
				result = new Bitmap(responseStream);
			}
			return result;
		}
		public string PostPage(string URL, string PostData)
		{
			return this.PostPage(URL, PostData, Encoding.UTF8);
		}
		public string PostPage(string URL, string PostData, Encoding encoding)
		{
			return this.PostPage(URL, PostData, encoding, null);
		}
		public string PostPage(string URL, string PostData, Encoding encoding, string ContentType)
		{
			this.IErrorTime = 0;
			return this._PostPage(URL, PostData, encoding, ContentType);
		}
		private string _PostPage(string URL, string PostData, Encoding encoding, string ContentType)
		{
			string result;
			try
			{
				if (ContentType == null)
				{
					ContentType = "application/x-www-form-urlencoded";
				}
				HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(URL);
				httpWebRequest.Proxy = this.Proxy;
				if (this.UserCookie == null)
				{
					this.UserCookie = new CookieContainer();
				}
				httpWebRequest.CookieContainer = this.UserCookie;
				httpWebRequest.Method = "POST";
				httpWebRequest.ContentType = ContentType;
				httpWebRequest.Headers.Add("Accept-Language", "zh-cn,zh");
				httpWebRequest.Headers.Add("Accept-Charset", "GB2312,utf-8");
				httpWebRequest.Accept = "text/html,application/xhtml+xml,application/xml";
				httpWebRequest.KeepAlive = true;
				httpWebRequest.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13 (.NET CLR 3.5.30729)";
				byte[] bytes = encoding.GetBytes(PostData);
				httpWebRequest.ContentLength = (long)bytes.Length;
				using (Stream requestStream = httpWebRequest.GetRequestStream())
				{
					try
					{
						requestStream.Write(bytes, 0, bytes.Length);
					}
					catch
					{
					}
				}
				HttpWebResponse myResponse = (HttpWebResponse)httpWebRequest.GetResponse();
				result = this._GetHTML(myResponse, encoding, 1024);
			}
			catch (Exception ex)
			{
				if (!ex.Message.Contains("连接") || this.IAfreshTime - this.IErrorTime <= 0)
				{
					throw;
				}
				this.IErrorTime++;
				result = this._PostPage(URL, PostData, encoding, ContentType);
			}
			return result;
		}
		private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
		{
			return true;
		}
		public string GetCookieStr(Uri url)
		{
			string result;
			if (this.UserCookie == null)
			{
				result = "";
			}
			else
			{
				result = this.UserCookie.GetCookieHeader(url);
			}
			return result;
		}
		public CookieCollection GetCookieCollection(Uri url)
		{
			CookieCollection result;
			if (this.UserCookie == null)
			{
				result = null;
			}
			else
			{
				result = this.UserCookie.GetCookies(url);
			}
			return result;
		}
	}
}
